﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace eTraining
{
    public static class StyleHelper
    {
        public static void SetWindowsRegion(Form frm)
        {
            if (frm.WindowState == FormWindowState.Maximized)
            {
                frm.Region = null;
                frm.Region = new Region(frm.Bounds);
                frm.Padding = new Padding(1);
            }
            else
            {
                GraphicsPath frmPath = GetRoundedRectPath(new Rectangle(0, 0, frm.Width, frm.Height), 8);
                frm.Padding = new Padding(6);
                frm.Region = null;
                frm.Region = new Region(frmPath);
            }

        }

        public static Cursor GetCursorForMode(int mode)
        {
            Cursor rst = Cursors.Default;

            switch (mode)
            {
                case 1:
                    rst = Cursors.SizeNWSE;
                    break;
                case 2:
                    rst = Cursors.SizeNS;
                    break;
                case 3:
                    rst = Cursors.SizeNESW;
                    break;
                case 4:
                    rst = Cursors.SizeWE;
                    break;
                case 5:
                    rst = Cursors.SizeNWSE;
                    break;
                case 6:
                    rst = Cursors.SizeNS;
                    break;
                case 7:
                    rst = Cursors.SizeNESW;
                    break;
                case 8:
                    rst = Cursors.SizeWE;
                    break;
                default :
                    break;
            }

            return rst;
        }

        public static int HitTestFormBorder(Point pt, Form frm)
        {
            int rst = -1;

            if (pt.X < frm.Padding.Left)
            {
                if (pt.Y < frm.Padding.Top)
                {
                    rst = 1;
                }
                else if (pt.Y > frm.Height - frm.Padding.Bottom)
                {
                    rst = 7;
                }
                else
                {
                    rst = 8;
                }
            }
            else if (pt.X > frm.Width - frm.Padding.Right)
            {
                if (pt.Y < frm.Padding.Top)
                {
                    rst = 3;
                }
                else if (pt.Y > frm.Height - frm.Padding.Bottom)
                {
                    rst = 5;
                }
                else
                {
                    rst = 4;
                }

            }
            else
            {
                if (pt.Y < frm.Padding.Top)
                {
                    rst = 2;
                }
                else if (pt.Y > frm.Height - frm.Padding.Bottom)
                {
                    rst = 6;
                }
            }

            return rst;
        }

        public static GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
        {
            int diameter = radius;
            Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
            GraphicsPath path = new GraphicsPath();
            //   左上角   
            path.AddArc(arcRect, 180, 90);
            //   右上角   
            arcRect.X = rect.Right - diameter;
            path.AddArc(arcRect, 270, 90);
            //   右下角   
            arcRect.Y = rect.Bottom - diameter;
            path.AddArc(arcRect, 0, 90);
            //   左下角   
            arcRect.X = rect.Left;
            path.AddArc(arcRect, 90, 90);
            path.CloseFigure();
            return path;
        }
    }
}
